<?php
function bierenshuo_get_db_config($const_table, $hash_key, $need_hash = true)
{
  /* 默认的基本信息 */
  $host = "127.0.0.1";
  $port = 3306;
  $user = "root";
  $pass = "FE73,DW)EW2[ECD.H!_F;OP";
  $database = "bierenshuo";
  $table = "";
  $table_alias = "";

  /* 自动生成拆库表的hash */
  $hash = $need_hash ? md5($hash_key) : $hash_key;

  switch ($const_table) {
        case "qun_recommend_thread":
            $host = "10.10.2.160";
            $user = "gang";
            $pass = "VOV1Y1YUWlc8LDRSZHiI";
            $database = "bbs_51_com"; // 数据库名
            $table = "bbs_post"; // 数据库中真实的表名
            $table_alias = "bbs_post"; // 表别名, sql 语句中使用此别名当表名
            break;
            
        case "bbs_notice":
            $host = "10.10.2.160";
            $user = "gang";
            $pass = "VOV1Y1YUWlc8LDRSZHiI";
            $database = "bbs_51_com"; // 数据库名
            $table = "bbs_notice"; // 数据库中真实的表名
            $table_alias = "bbs_notice"; // 表别名, sql 语句中使用此别名当表名
            break;
            
        case "bbs_post":
        	$index = hexdec(substr($hash, 0, 1));
        	$hosts = array(
        		'10.10.2.132',
        		'10.10.2.133',
        		'10.10.2.211',
        		'10.10.2.212'
        	);
            $host = $hosts[floor($index / 4)];
            $user = "gang";
            $pass = "VOV1Y1YUWlc8LDRSZHiI";
			$database = "db_bbs_post_".substr($hash, 0, 1); // 数据库名
			$table = "bbs_post_".substr($hash, 0, 3); // 数据库中真实的表名
			$table_alias = "bbs_post"; // 表别名, sql 语句中使用此别名当表名
            break;
            
        case "bbs_index":
            $host = "10.10.2.160";
            $user = "gang";
            $pass = "VOV1Y1YUWlc8LDRSZHiI";
			$database = "bbs_51_com"; // 数据库名
			$table = "bbs_index"; // 数据库中真实的表名
			$table_alias = "bbs_index"; // 表别名, sql 语句中使用此别名当表名
            break;
            
        case "bbs_category":
            $host = "10.10.2.160";
            $user = "gang";
            $pass = "VOV1Y1YUWlc8LDRSZHiI";
			$database = "bbs_51_com"; // 数据库名
			$table = "bbs_category"; // 数据库中真实的表名
			$table_alias = "bbs_category"; // 表别名, sql 语句中使用此别名当表名
            break;
            
        case 'my_thread':
            $host = "10.10.2.160";
            $user = "gang";
            $pass = "VOV1Y1YUWlc8LDRSZHiI";
            $database = "db_bbs_thread_my_".substr($hash, 0, 1); // 数据库名
            $table = "bbs_thread_my_".substr($hash, 0, 2); // 数据库中真实的表名
            $table_alias = "bbs_thread_my"; // 表别名, sql 语句中使用此别名当表名
            break;
            
        case 'bbs_thread':
        	$hosts = array(
        		'10.10.2.137',
        		'10.10.2.129'
        	);
            $host = $hosts[$hash % 2];
            $user = "gang";
            $pass = "VOV1Y1YUWlc8LDRSZHiI";
            $database = "db_bbs_thread"; // 数据库名
            $table = "bbs_thread_{$hash}"; // 数据库中真实的表名
            $table_alias = "bbs_thread"; // 表别名, sql 语句中使用此别名当表名
            break;
            
        case 'operation_log':
            $host = "10.10.2.160";
            $user = "gang";
            $pass = "VOV1Y1YUWlc8LDRSZHiI";
            $database = "bbs_51_com"; // 数据库名
            $table = "operate_log"; // 数据库中真实的表名
            $table_alias = "operate_log"; // 表别名, sql 语句中使用此别名当表名
            break;
            
        case 'thread_log':
            $host = "10.10.2.160";
            $user = "gang";
            $pass = "VOV1Y1YUWlc8LDRSZHiI";
            $database = "bbs_51_com"; // 数据库名
            $table = "thread_log"; // 数据库中真实的表名
            $table_alias = "thread_log"; // 表别名, sql 语句中使用此别名当表名
            break;
            
        case 'user':
            $host = "10.10.2.160";
            $user = "gang";
            $pass = "VOV1Y1YUWlc8LDRSZHiI";
            $database = "bbs_51_com"; // 数据库名
            $table = "user"; // 数据库中真实的表名
            $table_alias = "user"; // 表别名, sql 语句中使用此别名当表名
            break;
            
        case 'manager':
            $host = "10.10.2.160";
            $user = "gang";
            $pass = "VOV1Y1YUWlc8LDRSZHiI";
            $database = "bbs_51_com"; // 数据库名
            $table = "manager"; // 数据库中真实的表名
            $table_alias = "manager"; // 表别名, sql 语句中使用此别名当表名
            break;
             
        case 'special_comment':
            $host = "10.10.2.160";
            $user = "gang";
            $pass = "VOV1Y1YUWlc8LDRSZHiI";
            $database = "bbs_51_com"; // 数据库名
            $table = "special_comment"; // 数据库中真实的表名
            $table_alias = "special_comment"; // 表别名, sql 语句中使用此别名当表名
            break;	
            
        case 'bbs_post_reply':
        	$index = hexdec(substr($hash, 0, 1));
        	$hosts = array(
        		'10.10.2.132',
        		'10.10.2.133',
        		'10.10.2.211',
        		'10.10.2.212'
        	);
            $host = $hosts[floor($index / 4)];
            $user = "gang";
            $pass = "VOV1Y1YUWlc8LDRSZHiI";
			$database = "db_bbs_post_reply_".substr($hash, 0, 1); // 数据库名
			$table = "bbs_post_reply_".substr($hash, 0, 3); // 数据库中真实的表名
			$table_alias = "bbs_post_reply"; // 表别名, sql 语句中使用此别名当表名
            break;
        case 'test':
        	$host = "127.0.0.1";
            $user = "root";
            $pass = "123";
            $database = "bierenshuo"; // 数据库名
            $table = "test"; // 数据库中真实的表名
            $table_alias = "test"; // 表别名, sql 语句中使用此别名当表名
        	break;
        	
        default:
            return trigger_error("undefined table: ".$table, E_USER_ERROR);
  }

  if (DEVELOPMENT) {
        $host = "127.0.0.1";
        $port = 3306;
        $user = "root";
        $pass = "123";
  }

  /* 最终返回的信息, 以下字段为必须返回的 */
  return array(
      'host'      => $host.($port ? ":$port" : ""),
      'port'      => $port,
      'user'      => $user,
      'pass'      => $pass,
      'database'  => $database,
      'table'     => $table,
      'table_alias' => $table_alias
  );
}
?>